package com.ushine.design.design.b_structural_type.f_flyweight.a_practice;

import java.util.HashMap;

public class FlyweightFactory {
    //定义一个HashMap用于存储亨元对象，实现亨元池
    private HashMap flyweights = new HashMap<>();

    public Flyweight getFlyweight(String key) {
        //如果对象不存在，则直接从亨元池获取
        if (flyweights.containsKey(key)) {
            return (Flyweight) flyweights.get(key);
        }
        //如果对象不存在，先创建一个新的对象添加到亨元池中，然后返回
        else {
            ConcreteFlyweight flweight = new ConcreteFlyweight();
            flyweights.put(key, flweight);
            return flweight;
        }
    }
}
